﻿
#if !(UNITY_STANDALONE_WIN || UNITY_STANDALONE_LINUX || UNITY_STANDALONE_OSX || STEAMWORKS_WIN || STEAMWORKS_LIN_OSX)
#define DISABLESTEAMWORKS
#endif
#if !DISABLESTEAMWORKS
using Steamworks;
#endif
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.EventSystems;
using UnityEngine.UI;
public delegate void Callback();
public delegate void CallbackInt(int num);
public delegate void CallbackIntInt(int num1, int num2);
public delegate void CallbackFloat(float num);
public delegate void CallbackString(string value);
public delegate void CallbackVector3(Vector3 value);
public delegate void CallbackCollision2D(Collision2D value);
public delegate void CallbackCollider2D(Collider2D value);
public delegate void CallbackWeapon(Weapon weapon);
public delegate void CallbackUri(Uri value);
public class ShipCallback : UnityEvent<Ship> { }
[Serializable]
public class DiscoveryResponseCallback : UnityEvent<DiscoveryResponse> { };

public delegate Control.ShipOprate CallbackOprate(Vector3 value, int souce);
class UtilsUnity
{
    public enum Align
    {
        ALIGN_LEFT_TOP,
        ALIGN_RIGHT_TOP,
        ALIGN_LEFT_BOTTOM,
        ALIGN_RIGHT_BOTTOM,
        ALIGN_CENTER,
    }
    static public void Log(string log, bool isSystem)
    {
        if(!isSystem)
            Debug.Log(log);
    }
    /// <summary>
    /// 假定原点在右下角
    /// </summary>
    /// <param name="size"></param>
    /// <returns></returns>
    static public Vector3 GetPositionInScreen(Vector3 position, Vector2 size, Align align = Align.ALIGN_RIGHT_BOTTOM)
    {
        Vector3 mousePos = position + new Vector3(-10, -10);
        float scale = 1;
        var canvas = GetCanvas();
        if (null != canvas)
            scale = canvas.scaleFactor;
        size *= scale;

        switch (align)
        {
            case Align.ALIGN_LEFT_TOP:
                mousePos.x = Mathf.Clamp(mousePos.x, 0, Screen.width- size.x);
                mousePos.y = Mathf.Clamp(mousePos.y, size.y, Screen.height);
                break;
            case Align.ALIGN_RIGHT_TOP:
                mousePos.x = Mathf.Clamp(mousePos.x, size.x, Screen.width);
                mousePos.y = Mathf.Clamp(mousePos.y, size.y, Screen.height);
                break;
            case Align.ALIGN_LEFT_BOTTOM:
                mousePos.x = Mathf.Clamp(mousePos.x, 0, Screen.width - size.x);
                mousePos.y = Mathf.Clamp(mousePos.y, 0, Screen.height - size.y);
                break;
            case Align.ALIGN_RIGHT_BOTTOM:
                mousePos.x = Mathf.Clamp(mousePos.x, size.x, Screen.width);
                mousePos.y = Mathf.Clamp(mousePos.y, 0, Screen.height - size.y);
                break;
            case Align.ALIGN_CENTER:
                mousePos.x = Mathf.Clamp(mousePos.x, size.x/2, Screen.width - size.x / 2);
                mousePos.y = Mathf.Clamp(mousePos.y, size.y/2, Screen.height - size.y/2);
                break;
        }
        return mousePos;
    }


    public static Canvas GetCanvas()
    {

        var foundCanvas = GameObject.FindGameObjectsWithTag("Canvas");
        foreach (var canvas in foundCanvas)
        {
            if (canvas.activeSelf && canvas.GetComponent<Canvas>())
                return canvas.GetComponent<Canvas>();
        }
        return null;
    }

    public static float GetCanvasScale()
    {
        float scale = 1;
        var canvas = GetCanvas();
        if (canvas)
            scale = canvas.scaleFactor;
        return scale;
    }


    public static void SetGUIActive(GameObject gameObject, bool isVisible)
    {
        // 是否可见
        float alpha = isVisible ? 1 : 0;
        // 是否可点击
        bool isClick = isVisible ? true : false;

        CanvasGroup canvasGroup = gameObject.GetComponent<CanvasGroup>();
        if (!canvasGroup)
        {
            canvasGroup = gameObject.AddComponent<CanvasGroup>();
        }

        canvasGroup.alpha = alpha;
        canvasGroup.blocksRaycasts = isClick;
    }
    public static bool IsGUIActive(GameObject gameObject)
    {
        CanvasGroup canvasGroup = gameObject.GetComponent<CanvasGroup>();
        if (!canvasGroup)
        {
            canvasGroup = gameObject.AddComponent<CanvasGroup>();
        }

        return canvasGroup.alpha>0.01;
    }
    static public bool IsOnUI(Vector3 pos)
    {
        var canvas = GetCanvas();
        if (canvas == null)
            return false;
        GraphicRaycaster caster = canvas.GetComponent<GraphicRaycaster>();
        if (caster == null)
            return false;

        PointerEventData pData = new PointerEventData(EventSystem.current);

        pData.position = pos;
        pData.delta = Vector2.zero;
        pData.scrollDelta = Vector2.zero;

        List<UnityEngine.EventSystems.RaycastResult> canvasHits = new List<UnityEngine.EventSystems.RaycastResult>();
        caster.Raycast(pData, canvasHits);


        return canvasHits.Count > 0;
    }

    /// <summary>
    /// 以IO方式进行加载
    /// </summary>
    static public Sprite LoadSpriteByIo(string url, Vector2 piovt, float pixelPerUnit = 1)
    {
        //创建文件读取流
        FileStream fileStream = new FileStream(url, FileMode.Open, FileAccess.Read);
        //创建文件长度缓冲区
        byte[] bytes = new byte[fileStream.Length];
        //读取文件
        fileStream.Read(bytes, 0, (int)fileStream.Length);

        //释放文件读取流
        fileStream.Close();
        //释放本机屏幕资源
        fileStream.Dispose();
        fileStream = null;

        //创建Texture
        int width = 256;
        int height = 256;
        Texture2D texture = new Texture2D(width, height);
        texture.LoadImage(bytes);
        //创建Sprite
        Sprite sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), piovt, pixelPerUnit);
        
        return sprite;

    }
}